package msu.erdc.hci;

import msu.erdc.hci.customObjects.ContactObject;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class AContact extends Fragment {
	
	EditText lName;
	EditText fName;
	EditText phoneNum;
	Button cancelButton;
	EditText categoryText;
	EditText officePhone;
	EditText mobilePhone;
	EditText email;
	EditText address1;
	EditText address2;
	ContactObject newcontact;
	boolean canceled;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view =  inflater.inflate(R.layout.addcontact, container, false);

		lName = (EditText) view.findViewById(R.id.lnameText);
		fName = (EditText) view.findViewById(R.id.nameText);
		categoryText = (EditText) view.findViewById(R.id.categoryText);
		officePhone = (EditText) view.findViewById(R.id.officephoneText);
		
		mobilePhone = (EditText) view.findViewById(R.id.cellphoneText);
		email = (EditText) view.findViewById(R.id.emailText);
		address1 = (EditText) view.findViewById(R.id.addressText);
		address2 = (EditText) view.findViewById(R.id.address2Text);
		
		
		 cancelButton = (Button) view.findViewById(R.id.cancelButton);
		 // if emergency button is clicked
		 if(cancelButton.isClickable()==true){
		 cancelButton.setOnClickListener(new OnClickListener(){
							public void onClick(View v) {
//								newcontact.firstname=fName.getText().toString();
//								newcontact.lastname = lName.getText().toString();
//								newcontact.category = ":"+categoryText.getText().toString();
//								newcontact.mobile=mobilePhone.getText().toString();
//								newcontact.lastname = lName.getText().toString();
//								newcontact.firstname = fName.getText().toString();
//								newcontact.phone = officePhone.getText().toString();
//								newcontact.emailaddress = email.getText().toString();
//								newcontact.street =  address1.getText().toString();
//								newcontact.cityState = address2.getText().toString();
//								((Global)getActivity().getApplication()).contacts.add(newcontact);
//								FragmentTransaction ft = getFragmentManager().beginTransaction();
//								ft.replace(android.R.id.content, new Contacts(), "contacts");
//								//Contacts.counter =  Contacts.counter+1 ;
//								ft.addToBackStack(null);
//								ft.commit();
								canceled = true;
								FragmentManager man = getActivity().getFragmentManager();
								man.popBackStackImmediate();
								
							}});
		 }
		 return view;	
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		newcontact = new ContactObject();
		canceled = false;
	}
	
	@Override
	public void onDestroy()
	{
		if(!canceled)
		{
			newcontact.firstname=fName.getText().toString();
			newcontact.lastname = lName.getText().toString();
			newcontact.category = ":"+categoryText.getText().toString();
			newcontact.mobile=mobilePhone.getText().toString();
			newcontact.lastname = lName.getText().toString();
			newcontact.firstname = fName.getText().toString();
			newcontact.phone = officePhone.getText().toString();
			newcontact.emailaddress = email.getText().toString();
			newcontact.street =  address1.getText().toString();
			newcontact.cityState = address2.getText().toString();
			((Global)getActivity().getApplication()).contacts.add(newcontact);
		}
		super.onDestroy();
	}

}
	

